package com.g2room.io.grep;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Random;
import java.util.stream.IntStream;

public class GrepIysw extends GrepCommon {
    //    https://www.iysw.net/xy/index_190.html
    public static String domain = "https://www.iysw.net";
    public static String dir = "E:/资料/TXT01/";

    public static void main(String[] args) {
//        try {
//            downloadNet("https://www.iysw.net/e/DownSys/doaction.php?enews=DownSoft&classid=75&id=32974&pathid=0&pass=a175ffec58e1ac41e117e505c6a06104&p=:::", "11.txt");
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
        pages();
    }

    public static void pages() {
        String url = domain + "/xy/index%s.html";
        IntStream.range(0, 191).forEach(i -> parsePage(String.format(url, i == 0 ? "" : "_" + i)));
    }

    public static void parsePage(String url) {
        try {
            Document document = getDoc(url);
            Elements elements = document.select("table table table table a");
            for (Element e : elements) {
                try {
                    parseDetail(domain + e.attr("href"), e.text().replace("《", "").replace("》", ""));
                } catch (Exception e1) {
                    System.out.println(url + "::" + e1.getMessage());
                }
            }
            Thread.sleep(500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void parseDetail(String url, String title) {
        Document document = getDoc(url);
        Elements elements = document.select("#downAddress a");
        // <a href="#ecms" onclick="window.open('/e/DownSys/DownSoft/?classid=75&amp;id=32974&amp;pathid=0','','width=900,height=900,resizable=yes');">下载地址1</a>
        String downloadPage = domain + elements.get(new Random().nextInt(2)).attr("onclick").replace("window.open('", "").replace("','','width=900,height=900,resizable=yes');", "");
        try {
            // 获取下载页面并下载
            downloadTxt(downloadPage, title);
            // 获取封面
            Elements imgEl = document.select("#downInfoArea table tr td img");
            // 获取详情
            elements = document.select(".downInfoRowR");
            String imgAdd = imgEl.get(1).attr("src");
            StringBuilder sb = new StringBuilder();
            sb.append(imgAdd.endsWith("notimg.gif") ? "" : "图片地址：" + domain + imgAdd);
            sb.append("\r" + elements.get(0).html().replaceAll("<b>", "").replaceAll("</b>", "").replaceAll("<br>", "\r"));
            elements = document.select("#mainSoftIntro");
            sb.append("\r内容简介：" + elements.get(0).html());
            // 保存正常则在保存基本信息
            FileOutputStream fs = null;
            try {
                fs = new FileOutputStream(dir + title + "_summary.txt");
                fs.write(sb.toString().getBytes());
            } catch (IOException e) {
                System.out.println("文件不存在：" + e.getMessage());
            } finally {
                if (null != fs) {
                    try {
                        fs.close();
                    } catch (IOException e) {
                        System.out.println("关闭FS异常：" + e.getMessage());
                    }
                }
            }
        } catch (IOException e) {
            System.out.println("下载文件异常：" + e.getMessage());
        }
    }

    public static void downloadTxt(String url, String title) throws MalformedURLException {
        Document document = getDoc(url);
        Elements elements = document.select("a");
        // <a href="#ecms" onclick="window.open('/e/DownSys/DownSoft/?classid=75&amp;id=32974&amp;pathid=0','','width=900,height=900,resizable=yes');">下载地址1</a>
        String downloadUrl = domain + elements.get(0).attr("href").replace("../", "/e/DownSys/");
        // 获取下载页面并下载
        downloadNet(downloadUrl, title + ".rar");
    }
}
